﻿package com.air.swfmessengergui.display.slices
{
	
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.NativeWindow;

import com.air.swfmessengergui.SWFMessenger_gui;

public class Slice extends MovieClip
{
	private var mv:Boolean = false;
	private var mx:int, my:int, lx:int,ly:int;
	
	public function Slice() {
		mx = SWFMessenger_gui(root).marginLeft;
		my = SWFMessenger_gui(root).marginTop;
		addEventListener(MouseEvent.MOUSE_DOWN, mdow);
	}
	
	
	private function mdow(e:MouseEvent):void {
		var nw:NativeWindow = stage.nativeWindow;
		if (x==mx || y==my) {//slice de canto, n redimenciona a janela
			if (stage) {
				nw.startMove();
			}
		} else {
			if (stage) {//redimenciona
				mv = true;
				lx = nw.width-e.stageX;
				ly = nw.height-e.stageY;
				stage.addEventListener(MouseEvent.MOUSE_MOVE, mmov);
				stage.addEventListener(MouseEvent.MOUSE_UP, mup);
				stage.addEventListener(MouseEvent.ROLL_OUT, mup);
			}
		}
	}
	
	private function mmov(e:MouseEvent):void {
		var nv:NativeWindow = stage.nativeWindow;
		nv.width = e.stageX + lx;
		nv.height = e.stageY + ly;
		if (e.stageX>=nv.width || e.stageY>=nv.height) endResize();
	}
	
	private function mup(e:MouseEvent):void {
		endResize();
	}
	
	private function endResize():void {
		mv = false;
		stage.removeEventListener(MouseEvent.MOUSE_MOVE, mmov);
		stage.removeEventListener(MouseEvent.MOUSE_UP, mup);
		stage.removeEventListener(MouseEvent.ROLL_OUT, mup);
		
		SWFMessenger_gui(root).redraw(null);
	}
	
}
}